package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jdk.nashorn.internal.runtime.SharedPropertyMap;
import lombok.extern.slf4j.Slf4j;
import org.example.common.BaseContext;
import org.example.common.R;
import org.example.entity.ShoppingCart;
import org.example.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        if(shoppingCart.getDishId()!=null){
            Long dishId = shoppingCart.getDishId();
            queryWrapper.eq(ShoppingCart::getUserId,currentId).eq(ShoppingCart::getDishId,dishId);
        }else {
            Long setmealId = shoppingCart.getSetmealId();
            queryWrapper.eq(ShoppingCart::getUserId,currentId).eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if(cart==null){
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(currentId);
            shoppingCartService.save(shoppingCart);
            cart=shoppingCart;
        }else{
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
        }
        return R.success(cart);
    }

    /**
     * 删除菜单
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        if(shoppingCart.getDishId()!=null){
            Long dishId = shoppingCart.getDishId();
            queryWrapper.eq(ShoppingCart::getUserId,currentId).eq(ShoppingCart::getDishId,dishId);
        }else {
            Long setmealId = shoppingCart.getSetmealId();
            queryWrapper.eq(ShoppingCart::getUserId,currentId).eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        Integer number = cart.getNumber();
        if(number==1){
            shoppingCartService.removeById(cart.getId());
            cart.setNumber(0);
        }else{
            cart.setNumber(number-1);
            shoppingCartService.updateById(cart);
        }
        return R.success(cart);
    }

    /**
     *查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userid);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCarts);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功");
    }
}
